﻿Imports System.Collections.Generic
Imports System.Text
Imports Microsoft.VisualStudio.DebuggerVisualizers
Imports System.Windows.Forms
Imports System.Data.Common
Imports System.Drawing
Imports System.IO
Imports System.Reflection

<Assembly: System.Diagnostics.DebuggerVisualizer(
                                                GetType(Visualizers.ListVisualizer),
                                                GetType(Visualizers.ListVisulizersource),
                                                Target:=GetType(System.Collections.Generic.list(Of )),
                                                Description:="Visualize in a Grid")> 
Namespace Visualizers
    Public Class ListVisualizer
        Inherits DialogDebuggerVisualizer

        Protected Overrides Sub Show(windowService As Microsoft.VisualStudio.DebuggerVisualizers.IDialogVisualizerService, objectProvider As IVisualizerObjectProvider)
            Dim frm As New FormGridHost
            Using DS As New DataSet
                DS.ReadXml(objectProvider.GetData(), XmlReadMode.ReadSchema)
                frm.SourceDataset = DS
                windowService.ShowDialog(frm)
            End Using
        End Sub

        Public Shared Sub TestShow(Dataobject As Object)
            Dim dv As New VisualizerDevelopmentHost(Dataobject, GetType(Visualizers.ListVisualizer), GetType(Visualizers.ListVisulizersource))
            dv.ShowVisualizer()
        End Sub
    End Class

    Public Class ListVisulizersource
        Inherits VisualizerObjectSource

        Public Overrides Sub GetData(target As Object, outgoingData As System.IO.Stream)
            Dim Objtype As System.Type = target.GetType
            Dim basetype As Type
            If Objtype.IsArray Then
                basetype = Objtype.GetElementType
            Else
                basetype = Objtype.GetGenericArguments(0)
            End If
            Dim xmlstr As String = ""
            Using Ds As New DataSet
                Using tbl As New DataTable("List")
                    If basetype.IsPrimitive Or basetype.IsEnum Or basetype.IsArray Then
                        tbl.Columns.Add("Values")
                        For Each obj In target
                            tbl.Rows.Add(obj)
                        Next
                    ElseIf basetype.IsClass Or basetype.IsInterface Then
                        For Each Prop As PropertyInfo In basetype.GetProperties
                            tbl.Columns.Add(Prop.Name)
                        Next
                        For Each obj In target
                            Dim dr As DataRow = tbl.Rows.Add
                            For Each col As DataColumn In tbl.Columns
                                Dim objval As Object
                                Try
                                    'objval = obj.GetType().GetProperty(col.ColumnName).GetValue(obj, Nothing)
                                    objval = CallByName(obj, col.ColumnName, CallType.Get)
                                Catch ex As Exception
                                    objval = obj.ToString
                                End Try
                                If objval IsNot Nothing Then
                                    dr(col.ColumnName) = objval.ToString
                                Else
                                    dr(col.ColumnName) = "<Null>"
                                End If
                            Next
                        Next
                    Else
                        tbl.Columns.Add("Error")
                        tbl.Rows.Add("No transformation found implement to your own")
                    End If
                    Ds.Tables.Add(tbl)
                End Using
                Ds.WriteXml(outgoingData, XmlWriteMode.WriteSchema)
            End Using

        End Sub
    End Class

End Namespace
